﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Bastanteo.Domain;

namespace Bastanteo.Persistencia.ADO
{
    public class BastanteosDAO: IbastanteoDAO 
    {
        public Bastanteos Crear(Bastanteos r)
        {
            using (SqlConnection con = new SqlConnection(Conexion.ObtenerCadena()))
            {

                //if (r.RepresentanteNumDoc != null)
                //{
                con.Open();
                string sql = "insert into bastanteos values (@bid,@cid, @pid, @gid, @for, @com, @lim, @vig)";
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    com.Parameters.AddWithValue("@bid", r.BastanteoId );
                    com.Parameters.AddWithValue("@cid", r.ClienteId);
                    com.Parameters.AddWithValue("@pid", r.PoderesId);
                    com.Parameters.AddWithValue("@gid", r.GrupoId);
                    com.Parameters.AddWithValue("@for", r.BastanteoForma );
                    com.Parameters.AddWithValue("@com", r.BastanteoCombinacion );
                    com.Parameters.AddWithValue("@lim", r.BastanteoLimite );
                    com.Parameters.AddWithValue("@vig", r.BastanteoVigencia );
                    com.ExecuteNonQuery(); 

                }
                //}
            }
            return Obtener(r.BastanteoId  );
        }
        public Bastanteos Obtener(int id)
        {
            Bastanteos o = null;
            using (SqlConnection con = new SqlConnection(Conexion.ObtenerCadena()))
            {
                con.Open();
                string sql = "select * from Bastanteos where BastanteoId=@d";
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    com.Parameters.AddWithValue("@d", id);
                    SqlDataReader r = com.ExecuteReader();
                    if (r.Read())
                    {
                        o = new Bastanteos();
                        o.BastanteoId = (int)r["BastanteoId"];
                        o.ClienteId = (string)r["ClienteId"];
                        o.PoderesId = (string)r["PoderesId"];
                        o.GrupoId = (int)r["GrupoId"];
                        o.BastanteoForma = (string)r["BastanteoForma"];
                        o.BastanteoCombinacion = (string)r["BastanteoCombinacion"];
                        o.BastanteoLimite = (double)r["BastanteoLimite"];
                        o.BastanteoVigencia = (DateTime)r["BastanteoVigencia"];

                    }
                }
            }
            return o;
        }


        public Bastanteos ObtenerPorCliente(string ClienteId)
        {
            Bastanteos o = null;
            using (SqlConnection con = new SqlConnection(Conexion.ObtenerCadena()))
            {
                con.Open();
                string sql = "select * from Bastanteos where ClienteId=@d";
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    com.Parameters.AddWithValue("@d", ClienteId);
                    SqlDataReader r = com.ExecuteReader();
                    if (r.Read())
                    {
                        o = new Bastanteos();
                        o.BastanteoId = (int)r["BastanteoId"];
                        o.ClienteId = (string)r["ClienteId"];
                        o.PoderesId = (string)r["PoderesId"];
                        o.GrupoId = (int)r["GrupoId"];
                        o.BastanteoForma = (string)r["BastanteoForma"];
                        o.BastanteoCombinacion = (string)r["BastanteoCombinacion"];
                        o.BastanteoLimite = (int)r["BastanteoLimite"];
                        o.BastanteoVigencia = (DateTime)r["BastanteoVigencia"];

                    }
                }
            }
            return o;
        }


        public Bastanteos  Modificar(Bastanteos  r)
        {
            using (SqlConnection con = new SqlConnection(Conexion.ObtenerCadena()))
            {
                con.Open();
                string sql = "update bastanteos set  ClienteId=@cid, PoderesId=@pid, GrupoId=@gid, BastanteoForma=@for,BastanteoCombinacion=@com, BastanteoLimite=@lim, BastanteoVigencia=@vig where BastanteoId=@bid";
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    com.Parameters.AddWithValue("@bid", r.BastanteoId);
                    com.Parameters.AddWithValue("@cid", r.ClienteId);
                    com.Parameters.AddWithValue("@pid", r.PoderesId);
                    com.Parameters.AddWithValue("@gid", r.GrupoId);
                    com.Parameters.AddWithValue("@for", r.BastanteoForma);
                    com.Parameters.AddWithValue("@com", r.BastanteoCombinacion);
                    com.Parameters.AddWithValue("@lim", r.BastanteoLimite);
                    com.Parameters.AddWithValue("@vig", r.BastanteoVigencia);
                    com.ExecuteNonQuery();
                }
            }
            return Obtener(r.BastanteoId );
        }
        public void Eliminar(Bastanteos  r)
        {
            using (SqlConnection con = new SqlConnection(Conexion.ObtenerCadena()))
            {
                con.Open();
                string sql = "delete from bastanteos where BastanteoId=@d";
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    com.Parameters.AddWithValue("@d", r.BastanteoId);
                    com.ExecuteNonQuery();
                }
            }
        }
       
        
        public ICollection<Bastanteos> Listar()
        {


            ICollection<Bastanteos> _todos = null;
            Bastanteos o = null;

            using (SqlConnection con = new SqlConnection(Conexion.ObtenerCadena()))
            {
                con.Open();

                string sql = "select * from bastanteos";
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    SqlDataReader r = com.ExecuteReader();
                    if (r.Read())
                    {
                        o = new Bastanteos();
                        o.BastanteoId = (int)r["BastanteoId"];
                        o.ClienteId = (string)r["ClienteId"];
                        o.PoderesId = (string)r["PoderesId"];
                        o.GrupoId = (int)r["GrupoId"];
                        o.BastanteoForma = (string)r["BastanteoForma"];
                        o.BastanteoCombinacion = (string)r["BastanteoCombinacion"];
                        o.BastanteoLimite = (int)r["BastanteoLimite"];
                        o.BastanteoVigencia = (DateTime)r["BastanteoVigencia"];

                        _todos.Add(o);
                    }
                }
            }

            return _todos;
        }

        #region IbaseDao<Bastanteos,int> Members


  

        #endregion
    }
}